/**
 * 开放接口控制器
 */

import path from 'path'
import fs from 'fs'

class Open {
    /**
     * 首页
     */
    async home (ctx) {
        const { config } = ctx.server;
        ctx.type = 'html';
        const html = fs.readFileSync(path.join(config.staticPath, 'index.html'))
        ctx.body = html
    }
    /**
     * 用户登录
     */
    async login(ctx) {
        const { server } = ctx;
        const { account, password } = ctx.request.body;
        ctx.params = { account, password };
        const result = await server.service.open.login(ctx);
        ctx.$response(result);
    }
    /**
     * 用户注销
     */
    async logout(ctx) {
        const { server } = ctx;
        // 清除token
        await server.helper.context.token.clear(ctx.tokenstr)
        ctx.$response({
            code: 200,
            msg: 'logout success'
        });
    }
}

export default Open